package com.supremex.aisupremex.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.supremex.aisupremex.web.entity.Topic;
import com.supremex.aisupremex.web.service.TopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zkx
 * @Date 2024/4/9 18:51
 */
@RequestMapping("/topic")
@Controller
public class TopicController {

	@Autowired
	private TopicService topicService;

	@RequestMapping("/list")
	public String topicList(Model model){
		List<String> topics = new ArrayList<>();
		topics.add("c");
		topics.add("2.BIO、NIO、AIO有什么区别？");
		topics.add("3.Exception和Error有什么区别？");
		topics.add("4.final、finally、finalize有什么区别？");
		topics.add("5.Java是值传递还是引用传递？");

		model.addAttribute("topics",topics);
		return "topic";
	}

	@RequestMapping("/next")
	public String topicNext(Model model){

		model.addAttribute("topic",getRandomTopic());
		return "topic";
	}

	@RequestMapping("/dailyTopic")
	public String dailyTopic(Model model){
		List<Topic> topics = topicService.listRandomTopics(10);
		model.addAttribute("topics",topics);
		return "daily_topic";
	}

	private String getRandomTopic(){
		List<Topic> topics = topicService.list();

		return topics.stream().map(Topic::getTitle).collect(Collectors.toList()).get((int)(Math.random()*topics.size()));
	}
}
